﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_Frog : Enemy
{
    [Header("获取组件")]
    private Rigidbody2D rb;
    //private Animator anim;
    private Collider2D coll;
    [Header("移动参数")]
    public float Speed, jumpForce;
    [Header("地面参数")]
    public LayerMask playerMask, ground;
    [Header("设置左右边界")]
    private Transform Boundaries;
    private Transform leftPoint, rightPoint;
    private float left_x, right_x;
    /* [Header("朝向判定")]
    private bool Faceleft = true; */
    protected override void Start()
    {
        base.Start();//获取父类的start方法
        rb = GetComponent<Rigidbody2D>();
        //anim = GetComponent<Animator>();
        coll = GetComponent<Collider2D>();
        // 获取Frog的Transform子类组件
        Boundaries = transform.GetChild(0);
        leftPoint = Boundaries.GetChild(0);
        rightPoint = Boundaries.GetChild(1);
        transform.DetachChildren();

        // 记录两个边界点的X坐标值
        left_x = leftPoint.position.x;
        right_x = rightPoint.position.x;

        // 删除边界(Boundaries)对象
        Destroy(Boundaries.gameObject);
    }
    void FixedUpdate()
    {
        //Movement();
    }
    void Update()
    {
        switchAnim();
    }

    void Movement()
    {
        /* if (Faceleft)
        {
            rb.velocity = new Vector2(-Speed, rb.velocity.y);
            if (transform.position.x < leftpoint.position.x)
            {
                transform.localScale = new Vector3(-1, 1, 1);
                Faceleft = false;
            }
        }
        else
        {
            rb.velocity = new Vector2(Speed, rb.velocity.y);
            if (transform.position.x < leftpoint.position.x)
            {
                transform.localScale = new Vector3(1, 1, 1);
                Faceleft = true;
            }
        } */
        if (coll.IsTouchingLayers(ground))
        {
            anim.SetBool("jumping", true);
            rb.velocity = new Vector2(-transform.localScale.x * Speed, jumpForce);
        }

        if (transform.position.x < left_x || transform.position.x > right_x || rb.IsTouchingLayers(playerMask))
        {
            rb.velocity = new Vector2(rb.velocity.x * -1, rb.velocity.y);
            transform.localScale = new Vector3(transform.localScale.x * -1, 1, 1);
        }
    }

    void switchAnim()
    {
        if (anim.GetBool("jumping"))
        {
            if (rb.velocity.y < 0.1)
            {
                anim.SetBool("jumping", false);
                anim.SetBool("falling", true);
            }

        }
        if (coll.IsTouchingLayers(ground))
        {
            anim.SetBool("falling", false);
        }
    }

}
